package com.xindaoapp.happypet.activity;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.rockerhieu.emojicon.EmojiconHandler;
import com.xindaoapp.happypet.R;
import com.xindaoapp.happypet.XDHttpClient;
import com.xindaoapp.happypet.utils.CommonParameter.UserState;
import com.xindaoapp.happypet.utils.CommonUtil;
import com.xindaoapp.happypet.utils.Constants;
import com.xindaoapp.happypet.utils.xdConfig;

public class ReplayItemActivity extends UMBaseActivity implements OnClickListener {
    private final String TAG = "ReplayItemActivity";
    private EditText et_content;
    private ImageButton back;
    private TextView title;
    private TextView publish;
    private Context context;
    private String fid = null;
    // private String uid = null;
    private String tid = null;
    private String pid = null;

    private String str = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_post_replay_item);
        // uid = ConstantsValue.uid;
        fid = getIntent().getStringExtra("fid");
        tid = getIntent().getStringExtra("tid");
        pid = getIntent().getStringExtra("pid");
        context = getApplicationContext();

        et_content = (EditText) findViewById(R.id.content);
        back = (ImageButton) findViewById(R.id.cancel);
        publish = (TextView) findViewById(R.id.publish1);
        title = (TextView) findViewById(R.id.check_club);

        back.setOnClickListener(this);
        publish.setOnClickListener(this);

        initData();
    }

    private void initData() {
        et_content.setSelection(0);
        title.setText("回复");

        et_content.addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {}

            @Override
            public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {}

            @Override
            public void afterTextChanged(Editable arg0) {
                str = EmojiconHandler.getMEmojiCode();
            }
        });
    }

    private void getData(String str) {
        if (CommonUtil.checkNetState(context)) {
            RequestParams params = new RequestParams();
            params.put("tid", tid);
            params.put("uid", UserState.getUserUid());
            params.put("fid", fid);
            if (!TextUtils.isEmpty(pid)) {
                params.put("repquote", pid);
            }
            params.put("content", str);

            XDHttpClient.get(xdConfig.POSTREPLY, params, new AsyncHttpResponseHandler() {
                @Override
                public void onSuccess(int statusCode, String content) {
                    JSONObject obj;
                    int result = -1;
                    try {
                        obj = new JSONObject(content);
                        result = obj.getInt("result");
                    } catch (JSONException e1) {
                        e1.printStackTrace();
                    }

                    if (result == 0) {
                        Toast.makeText(context, "积分+2", Toast.LENGTH_SHORT).show();
                        sendBroadcast(new Intent(Constants.ACTION_REPLAY_SUCCESS));
                        finish();
                    } else {
                        Toast.makeText(context, "回复失败,请重新回复", Toast.LENGTH_SHORT).show();
                        return;
                    }

                }

                @Override
                public void onFailure(Throwable arg0, String arg1) {
                    super.onFailure(arg0, arg1);
                    Toast.makeText(context, R.string.get_error_data, 0).show();
                }
            });
        } else {
            Toast.makeText(context, R.string.pLease_check_network, 0).show();
            return;
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.cancel:
                finish();
                break;
            case R.id.publish1:
                if (TextUtils.isEmpty(str)) {
                    Toast.makeText(context, "请输入回复内容", Toast.LENGTH_SHORT).show();
                    return;
                }
                getData(str);
                break;
            default:
                break;
        }
    }
}
